package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.SetmealDao;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Transactional
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao,Setmeal> implements SetmealService {

@Autowired
private SetmealService setmealService;
@Autowired
private SetmealDishService setmealDishService;
@Autowired
private CategoryService categoryService;
/*@Autowired
private DishService dishService;*/


    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void saveWithDish(SetmealDto setmealDto) {
        //先把模型存到套餐表
        setmealService.save(setmealDto);
        //再把菜品存到中间表
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list.stream().forEach(c->{c.setSetmealId(setmealDto.getId());});
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(list);
    }

    @Override
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //封装Page对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        if(name!=null&&name!=""){
            lqw.like(Setmeal::getName,name);
        }
        lqw.orderByDesc(Setmeal::getUpdateTime);
        lqw.eq(Setmeal::getIsdeleted,0);
        Page<Setmeal> setmealPage = setmealService.page(pageInfo, lqw);
        //克隆属性
        //BeanUtils.copyProperties(setmealPage,dtoPage);


        //把菜品类型名称存入到page对象中
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtoList = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            //克隆属性
            BeanUtils.copyProperties(record,setmealDto);
            dtoList.add(setmealDto);
        }
        dtoPage.setRecords(dtoList);
        dtoPage.setTotal(setmealPage.getTotal());
        return dtoPage;
    }

    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void deleteIds(Long[] ids) {
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        //先逻辑删除套餐
        setmeals.stream().forEach(c->{
            c.setIsdeleted(1);
        });
        setmealService.updateBatchById(setmeals);
        //后逻辑删除中间表
        for (Setmeal setmeal : setmeals) {
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> dishList = setmealDishService.list(lqw);
            dishList.stream().forEach(c->{
                c.setIsDeleted(1);
            });
            setmealDishService.updateBatchById(dishList);
        }
    }

    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void updateStatus(int status, Long[] ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Setmeal::getId,id);
            Setmeal one = setmealService.getOne(lqw);
            one.setStatus(status);
            setmealService.updateById(one);
        }
    }

    @Override
    @Cacheable(value = "setmealCache",key = "#id")
    public SetmealDto findById(Long id) {
        //查询套餐信息
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //克隆属性
        BeanUtils.copyProperties(setmeal,setmealDto);
        //取出中间表
        LambdaQueryWrapper<SetmealDish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(lqw1);
        /*for (SetmealDish setmealDish : setmealDishes) {
            LambdaQueryWrapper<Dish> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(Dish::getId,setmealDish.getDishId());
            Dish dish = dishService.getOne(lqw2);
        }*/
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    @Override
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public void updateAll(SetmealDto setmealDto) {
        //清除中间表数据
        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(lqw);
        //再添加新的菜品到中间表
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.stream().forEach(c->{
            c.setSetmealId(id);

        });
        setmealDishService.saveBatch(setmealDishList);
        setmealService.updateById(setmealDto);
    }

    @Override
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public List<Setmeal> listByid(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus()!=null,Setmeal::getStatus,1);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lqw);
        return list;
    }
}
